app.controller("pondCtrl", [
    "$scope",
    "$http",
    "$modal",
    "$log",
    "$compile",
    "$rootScope",
    "$stateParams",
    "$location",
    "$timeout",
    "$filter",
    "lodash",
    function (
        $scope,
        $http,
        $modal,
        $log,
        $compile,
        $rootScope,
        $stateParams,
        $location,
        $timeout,
        $filter,
        lodash
    ) {
        //页面加载时，给headerElement设置元素
        $rootScope.initHeaderElements($scope.settings);
        $rootScope.initBreadcrumb($scope.breadcrumb);
        $scope.breadcrumb.push("用户故事");
        $scope.settings.searchEle = {
            show: true, //显示查询框
            func: function (keyword) {
                //点击查询框调用的方法
                $scope.urlKeyword = keyword ? keyword : "";
                searchIssueByKeyword();
            },
            blurFunc: function (keyword) {
                $scope.urlKeyword = keyword ? keyword : "";
            }
        };
        var addIssueBtn = {
            show: $rootScope.isMatrixRole(),
            label: "添加用户故事",
            func: function () {
                openModelForReq("lg");
            }
        };
        $scope.settings.dateRadios = {
            show: true,
            radios: config_date_radios,
            init: 2,
            func: function (startDate) {
                $scope.info.searchC.startDate = startDate;
                searchIssueByKeyword();
            }
        };
        var addReqBtn = {
            show: $rootScope.isMatrixCusRole("pdm"),
            label: "归纳产品设计",
            func: function () {
                addToDesign();
            }
        };
        if ($rootScope.isMatrixRole()) {
            $scope.settings.btnEle.push(addIssueBtn); //添加点击按钮
        }
        if ($rootScope.isMatrixCusRole("pdm")) {
            $scope.settings.btnEle.push(addReqBtn);
        }
        //结束headerElement设置

        $scope.issuesList = [];
        $scope.selected = [];
        $scope.testArray = [
            {id: 1, name: "aaa"},
            {id: 2, name: "bbb"},
            {id: 3, name: "ccc"}
        ];
        $scope.userList = [];
        $scope.urlType = ""; //url中的类型
        $scope.urlKeyword = ""; //url中的关键字
        $scope.urlAssign = ""; //url中的提出人
        $scope.urlToAccount = ""; //url中的接收人
        var isOperation = new Object();
        isOperation.Delete = {
            isTrue: $rootScope.isMatrixRole(),
            msg: "删除用户故事"
        };
        //init info
        $scope.info = {
            headerTitle: "用户故事",
            tableScope: {},
            searchC: {},
            reqThData: [
                {thName: "故事描述"},
                {thName: "分组标签"},
                {thName: "业务线"},
                // {"thName": "提出人"},
                {thName: "更新日期"},
                {thName: "负责人"},
                {thName: "状态"}
                // {"thName": "提出日期"}
            ],
            page: {isTrue: true, addIsTrue: true},
            reqList: {
                isOperation: isOperation,
                tdName: [
                    {name: "useraction"},
                    {name: "groupTag"},
                    {name: "typeName"},
                    // {"name": "creatorName"},
                    {name: "updateTime"},
                    {name: "toName"},
                    {name: "issueStatus"}
                    // {"name": "createTime"}
                ],
                trIsClick: false
            }
        };

        $scope.settings.switchs.push({
            label: "显示已上线用户故事",
            check: false,
            checkFun: function (check) {
                $scope.info.searchC.showOnline = check;
                searchIssueByKeyword();
            }
        });

        setFondness();
        getIssuseType();
        getUserArray();

        //获取偏好设置
        function setFondness() {
            var fondness = getFondness("/api/v1/issue/getIssues");
            if (fondness) {
                $scope.urlType = fondness.type ? fondness.type : "";
                $scope.urlAssign = fondness.creatorAccount
                    ? fondness.creatorAccount
                    : "";
                $scope.urlToAccount = fondness.toAccount ? fondness.toAccount : "";
                //$scope.urlKeyword   = fondness.keyword ? fondness.keyword : "";
                //$scope.settings.searchEle.value = fondness.keyword ? fondness.keyword : "";
            }
        }

        //获取用户集合
        function getUserArray() {
            var url = beetle_auth_url + "/user/?token_id=" + getTokenId();
            $http.get(url).success(function (result) {
                if (result.success) {
                    if (result.data) {
                        var resArray = [{crtname: "所有提出人", id: 0}];
                        var toAccountAry = [{crtname: "所有受理人", id: 0}];
                        angular.forEach(result.data, function (data) {
                            data.crtname = data.realname;
                            resArray.push(data);
                            toAccountAry.push(data);
                        });
                        setHeaderCreator(resArray);
                        setHeaderToAccount(toAccountAry);
                    } else {
                        $rootScope.toasterErrorInfo("获取用户数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.msg);
                }
            });
        }

        //设置header中的提出人
        function setHeaderCreator(userArray) {
            var fondnessAssign = "";
            if ($scope.urlAssign) {
                var crtAccount = lodash.findWhere(userArray, {
                    account: $scope.urlAssign
                });
                crtAccount.crtname = crtAccount.realname;
                fondnessAssign = crtAccount;
            } else {
                fondnessAssign = {crtname: "所有提出人", id: 0};
            }
            var userSelectEle = {
                status: true,
                items: userArray,
                prompt: "请选择提出人",
                selectedItem: fondnessAssign,
                func: function (item) {
                    $scope.urlAssign = item.account ? item.account : "";
                    searchIssueByKeyword();
                }
            };
            $scope.settings.uiSltEle.push(userSelectEle);
        }

        //设置header中的接收人
        function setHeaderToAccount(toAccountAry) {
            var fondnessAccount = "";
            if ($scope.urlToAccount) {
                var crtAccount = lodash.findWhere(toAccountAry, {
                    account: $scope.urlToAccount
                });
                crtAccount.crtname = crtAccount.realname;
                fondnessAccount = crtAccount;
            } else {
                fondnessAccount = {crtname: "所有受理人", id: 0};
            }
            var userSelectEle = {
                status: true,
                items: toAccountAry,
                prompt: "请选择指派对象",
                selectedItem: fondnessAccount,
                func: function (item) {
                    $scope.urlToAccount = item.account ? item.account : "";
                    searchIssueByKeyword();
                }
            };
            $scope.settings.uiSltEle.push(userSelectEle);
        }

        //设置header中的业务线
        function setHeaderIssueType(issuesList) {
            var fondnessIssueType = "";
            if ($scope.urlType) {
                var crtIssueType = lodash.findWhere(issuesList, {
                    key: $scope.urlType
                });
                fondnessIssueType = crtIssueType.value;
            } else {
                fondnessIssueType = "";
            }
            var issueSelectEle = {
                status: true,
                crtName: fondnessIssueType,
                label: "全部业务线",
                eleList: issuesList,
                func: function (item) {
                    issueSelectEle.crtName = item ? item.name : issueSelectEle.label;
                    $scope.urlType = item ? item.key : null;
                    searchIssueByKeyword();
                }
            };
            $scope.settings.selectEle.push(issueSelectEle);
        }

        //get issuse list
        function getIssuseType() {
            var url = sys_config_url + "/select/get?type=BRD_BUSINESS";
            $http.get(url).success(function (result) {
                if (result.code == "0") {
                    if (result.data) {
                        angular.forEach(result.data.BRD_BUSINESS, function (issue) {
                            issue.name = issue.value;
                        });
                        $scope.issuesList = result.data.BRD_BUSINESS;
                        getUserList(result.data.BRD_BUSINESS);
                        setHeaderIssueType(result.data.BRD_BUSINESS);
                    } else {
                        $rootScope.toasterErrorInfo("获取业务线数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }

        //获取用户集合（根据account获取用户的中文名称）
        function getUserList(issueTypeList) {
            var url = beetle_auth_url + "/user/?token_id=" + getTokenId();
            $http.get(url).success(function (result) {
                if (result.success) {
                    if (result.data) {
                        $scope.userList = result.data;
                        searchIssueByKeyword(issueTypeList, result.data);
                    } else {
                        $rootScope.toasterErrorInfo("获取用户数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.msg);
                }
            });
        }

        //类型查询 & 关键字查询
        function searchIssueByKeyword(issueTypeList, userList) {
            var url = matrix_product_url + "/issue/getIssues";
            var urlArray = [
                {type: "reserveHabit", param: 1},
                {type: "tokenId", param: getTokenId()},
                {type: "type", param: $scope.urlType},
                {type: "creatorAccount", param: $scope.urlAssign},
                {type: "toAccount", param: $scope.urlToAccount},
                {type: "keyword", param: $scope.urlKeyword}
            ];

            if ($scope.info.searchC.showOnline) {
                urlArray.push({
                    type: "withComplete",
                    param: $scope.info.searchC.showOnline ? "1" : "0"
                });
            }
            if ($scope.info.searchC.startDate) {
                urlArray.push({
                    type: "startDate",
                    param: $scope.info.searchC.startDate
                });
            }

            url = $rootScope.setUrlParams(url, urlArray);

            $timeout(function () {
                $http.get(url).success(function (result) {
                    if (result.code == "0") {
                        if (result.data) {
                            var issueTypeArray = issueTypeList
                                ? issueTypeList
                                : $scope.issuesList;
                            var userArray = userList ? userList : $scope.userList;
                            angular.forEach(result.data, function (data) {
                                angular.forEach(issueTypeArray, function (status) {
                                    if (status.key == data.type) {
                                        data.typeName = status.value;
                                    }
                                });
                                var creatorNameList = lodash.findWhere(userArray, {
                                    account: data.creatorAccount
                                });
                                var toNameList = lodash.findWhere(userArray, {
                                    account: data.toAccount
                                });
                                data.creatorName = creatorNameList
                                    ? creatorNameList.realname
                                    : data.creatorAccount;
                                data.toName = toNameList ? toNameList.realname : data.toAccount;

                                data.issueStatus = "";
                                angular.forEach(config_issue_status, function (status) {
                                    if (data.status == status.key) {
                                        data.issueStatus = status.value;
                                    }
                                })
                                data.useraction = data.user + data.action;
                                data.updateTime = timestampToTime(data.updateTime);
                                data.createTime = timestampToTime(data.createTime);
                            });
                            $scope.info.page.isTrue = result.data.length > 30 ? true : false;
                            $scope.info.reqList.datas = result.data;
                        } else {
                            $rootScope.toasterErrorInfo("获取issue列表数据错误！");
                        }
                    } else {
                        $rootScope.toasterErrorInfo(result.desc);
                    }
                });
            });
        }

        //新增模态框
        function openModelForReq(size) {
            var modalInstance = $modal.open({
                animation: true,
                templateUrl: getTemplateUrl("matrix/component/tpl/addReqModel.html"),
                controller: "addReqCtrl",
                size: size,
                keyboard: true,
                backdrop: true,
                resolve: {
                    issueType: function () {
                        return $scope.urlType;
                    }
                }
            });

            //点击modal新增后，把feature值传过来
            modalInstance.result.then(
                function (data) {
                    createReq(data);
                },
                function (reason) {
                    cancelModal(reason);
                }
            );
        }

        function createReq(data) {
            var postParams = {
                issues: data
            };
            var url = matrix_product_url + "/issue/saveIssues";
            $http({
                method: "post",
                url: url,
                data: JSON.stringify(postParams),
                headers: {"Content-Type": "application/json"}
            }).success(function (result) {
                if (result.code == "0") {
                    if (result.data) {
                        var issueTypeArray = $scope.issuesList;
                        var userArray = $scope.userList;
                        angular.forEach(result.data, function (data) {
                            angular.forEach(issueTypeArray, function (status) {
                                if (status.key == data.type) {
                                    data.typeName = status.value;
                                }
                            });
                            var creatorNameList = lodash.findWhere(userArray, {
                                account: data.creatorAccount
                            });
                            var toNameList = lodash.findWhere(userArray, {
                                account: data.toAccount
                            });
                            data.creatorName = creatorNameList
                                ? creatorNameList.realname
                                : data.creatorAccount;
                            data.toName = toNameList ? toNameList.realname : data.toAccount;
                            // data.createTime = timestampToTime(data.createTime);
                            data.issueStatus =
                                data.status == "INHAND"
                                    ? "新建"
                                    : data.status == "COMPLETE"
                                    ? "已完成"
                                    : "";
                        });
                        angular.forEach(result.data, function (d) {
                            d.useraction = d.content;
                            $scope.info.reqList.datas.push(d);
                        });
                        $rootScope.toasterSuccessInfo("用户故事创建成功！");
                        $scope.info.reqList.datas = $filter("orderBy")(
                            $scope.info.reqList.datas,
                            "createTime",
                            true
                        );
                    } else {
                        $rootScope.toasterErrorInfo("创建用户故事返回数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }

        //修改模态框
        $scope.openUpdateModal = function (info) {
            var modalInstance = $modal.open({
                animation: true,
                templateUrl: getTemplateUrl("matrix/component/tpl/updateModel.html"),
                controller: "updateCtrl",
                keyboard: true,
                backdrop: true,
                resolve: {
                    items: function () {
                        return info;
                    }
                }
            });

            //点击modal新增后，把feature值传过来
            modalInstance.result.then(
                function (data) {
                    updateData(data);
                },
                function (reason) {
                    cancelModal(reason);
                }
            );
        };

        function updateData(data) {
            data.typeName = lodash.findWhere($scope.issuesList, {
                key: data.type
            }).value;
            var url = matrix_product_url + "/issue/saveIssue";
            $http({
                method: "post",
                url: url,
                data: JSON.stringify(data),
                headers: {"Content-Type": "application/json"}
            }).success(function (result) {
                if (result.code == "0") {
                    angular.forEach($scope.info.reqList.datas, function (val) {
                        if (val.id == data.id) {
                            val = data;
                            val.useraction = val.user + val.action;
                        }
                    });
                    $rootScope.toasterSuccessInfo("用户故事修改成功！");
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }

        function cancelModal(reason) {
            console.log("取消");
        }

        //删除方法
        $scope.deleteIssue = function (info) {
            var confirmParams = {
                titleData: "提示",
                bodyData: "确定删除【" + info.action + "】吗？"
            };
            $rootScope.confirmWindow(confirmParams, function (data) {
                if (data) {
                    var url =
                        matrix_product_url + "/issue/deleteIssue?issueId=" + info.id;
                    $http.get(url).success(function (result) {
                        if (result.code == "0") {
                            lodash.remove($scope.info.reqList.datas, {id: info.id});

                            $rootScope.toasterSuccessInfo("删除成功！");
                        } else {
                            $rootScope.toasterErrorInfo(result.desc);
                        }
                    });
                }
            });
        };

        //归纳产品设计
        function addToDesign() {
            $scope.selected.length
                ? openDesignModal($scope.selected)
                : $rootScope.toasterWarningInfo("请选择要归纳的数据！");
        }

        function openDesignModal(selectedTrList) {
            var modalInstance = $modal.open({
                animation: true,
                templateUrl: getTemplateUrl("matrix/component/tpl/designModel.html"),
                controller: "designCtrl",
                size: "lg",
                keyboard: true,
                backdrop: true,
                resolve: {
                    data: function () {
                        return selectedTrList;
                    },
                    reqList: function () {
                        return $scope.info.reqList.datas;
                    }
                }
            });

            //点击modal新增后，把feature值传过来
            modalInstance.result.then(
                function (data) {
                    getIssueIdBySave(data);
                },
                function (reason) {
                    cancelModal(reason);
                }
            );
        }

        //保存表格数据，然后获取id
        function getIssueIdBySave(data) {
            angular.forEach(data, function (tr) {
                if (tr.sltUser) {
                    if (data.type != "tab") {
                        delete tr.sltUser;
                    }
                }
                if (tr.sltToAccount) {
                    if (data.type != "tab") {
                        delete tr.sltToAccount;
                    }
                }
            });
            var postParams = {issues: data.trData};
            var url = matrix_product_url + "/issue/saveIssues";
            $http({
                method: "post",
                url: url,
                data: JSON.stringify(postParams),
                headers: {"Content-Type": "application/json"}
            }).success(function (result) {
                if (result.code == "0") {
                    if (result.data) {
                        var isssues = "";
                        angular.forEach(result.data, function (res) {
                            isssues += res.id + ",";
                        });
                        createReqIssue(data, isssues);
                    } else {
                        $rootScope.toasterErrorInfo("批量创建Issue的返回数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }

        //创建req
        function createReqIssue(data, issuesList) {
            var postParams = {
                reqName: data.design.title,
                reqDesc: data.design.desc,
                issues: issuesList,
                productLineId: data.design.productLineId
            };
            var url = matrix_product_url + "/brdRequirement/saveBrdRequirement";
            $http({
                method: "post",
                url: url,
                data: JSON.stringify(postParams),
                headers: {"Content-Type": "application/json"}
            }).success(function (result) {
                if (result.code == "0") {
                    if (result.data) {
                        if (data.isChecked) {
                            window.location.href = "#/app/req/summary/" + result.data;
                        } else {
                            $rootScope.toasterSuccessInfo("操作成功！");
                        }
                    } else {
                        $rootScope.toasterErrorInfo("归纳产品设计时返回数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }
    }
]);

app.controller("addReqCtrl", [
    "$scope",
    "$rootScope",
    "$modalInstance",
    "$http",
    "$filter",
    "lodash",
    "issueType",
    function (
        $scope,
        $rootScope,
        $modalInstance,
        $http,
        $filter,
        lodash,
        issueType
    ) {
        $scope.info = {
            time: new Date(),
            dataList: [],
            issueList: [],
            brdList: [],
            proposeUser: {
                account: getUserAccount(),
                realname: getUserName(),
                id: getUserId()
            },
            assignUser: {
                account: getUserAccount(),
                realname: getUserName(),
                id: getUserId()
            },
            creator: {},
            toAccount: {},
            issueUser: [],
            issueItem: "", //当前选中的业务线
            brdItem: "", //当前选中的BRD
            userItem: "" //当前选中的用户
        };

        initUserItem();
        getIssueList();
        getIssueCode();

        //初始化用户
        function initUserItem() {
            angular.forEach(issueUser, function (user) {
                user.crtname = user.name;
            });
            $scope.info.issueUser = issueUser;
        }

        //获取业务线数据
        function getIssueList() {
            var url = sys_config_url + "/select/get?type=BRD_BUSINESS";
            $http.get(url).success(function (result) {
                if (result.code == "0") {
                    if (result.data) {
                        angular.forEach(result.data.BRD_BUSINESS, function (issueType) {
                            issueType.crtname = issueType.value;
                        });
                        $scope.info.issueList = result.data.BRD_BUSINESS;
                        //默认添加需求时，为业务线设置默认值【入库段】
                        $scope.info.issueItem = issueType
                            ? lodash.findWhere(result.data.BRD_BUSINESS, {key: issueType})
                            : result.data.BRD_BUSINESS[0];
                    } else {
                        $rootScope.toasterErrorInfo("获取业务线数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }

        //显示modal时，默认加载一条数据
        function getIssueCode() {
            var url = matrix_product_url + "/issue/getIssueNumber";
            $http.get(url).success(function (result) {
                if (result.code == "0") {
                    if (result.data) {
                        var initData = {
                            number: result.data,
                            user: "卖家",
                            sltUser: {name: "卖家", crtname: "卖家"},
                            action: "",
                            creatorName: getUserName(),
                            description: "",
                            toAccount: getUserAccount(),
                            sltToAccount: {
                                account: getUserAccount(),
                                realname: getUserName(),
                                id: getUserId()
                            }
                        };
                        $scope.info.dataList.push(initData);
                    } else {
                        $rootScope.toasterErrorInfo("获取IssueCode失败！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }

        //设置需求用户
        $scope.selectUserItem = function (item) {
            angular.forEach($scope.info.dataList, function (reqData) {
                if (reqData.number == item.data.number) {
                    reqData.user = item.name;
                }
            });
        };

        //点击添加行
        $scope.clickAddTr = function () {
            getIssueCode();
        };

        //设置需求内容
        $scope.blurAction = function (data) {
            angular.forEach($scope.info.dataList, function (reqData) {
                if (reqData.number == data.number) {
                    reqData.action = data.action;
                }
            });
        };

        //设置详述
        $scope.blurDesc = function (data) {
            angular.forEach($scope.info.dataList, function (reqData) {
                if (reqData.number == data.number) {
                    reqData.description = data.description;
                }
            });
        };

        //设置单独指派
        $scope.selectToAccount = function (user) {
            angular.forEach($scope.info.dataList, function (reqData) {
                if (reqData.number == user.data.number) {
                    reqData.toAccount = user.account;
                }
            });
        };

        //设置提出人
        $scope.selectProposeUser = function (user) {
            $scope.info.proposeUser = user;
            angular.forEach($scope.info.dataList, function (data) {
                data.creatorName = user.realname;
            });
        };

        //设置总指派人
        $scope.selectAssignUser = function (user) {
            $scope.info.assignUser = user;
            $scope.info.toAccount = user;
            angular.forEach($scope.info.dataList, function (reqData) {
                reqData.sltToAccount = user;
                reqData.toAccount = user.account;
            });
        };

        //删除行
        $scope.clickDeleteBtn = function (data) {
            var result = $scope.info.dataList;
            lodash.remove(result, function (reqData) {
                return reqData.number == data.number;
            });
            $scope.info.dataList = result;
        };

        $scope.info.showAlert = false;

        $scope.showAlertMessage = function () {
            $scope.info.showAlert = !$scope.info.showAlert;
            // $(".alert-danger").css("display", "block");
        };

        // $scope.hideAlertMessage = function() {
        //     $(".alert-danger").css("display", "none");
        // };

        $scope.createClick = function () {
            var accountList = "";
            if (!$scope.info.issueItem.value) {
                $(".form-type-select").focus();
                $rootScope.toasterWarningInfo("请选择业务线！");
                return false;
            }
            if (!$scope.info.proposeUser.account) {
                $(".col-ui-select")
                    .find(".ui-select-toggle")
                    .focus();
                $rootScope.toasterWarningInfo("请选择提出人！");
                return false;
            }
            angular.forEach($scope.info.dataList, function (req) {
                if (req.sltToAccount.account != undefined) {
                    accountList += req.sltToAccount.account;
                }
            });
            if (!$scope.info.assignUser.account) {
                if (!accountList) {
                    $(".col-assign-select")
                        .find(".ui-select-toggle")
                        .focus();
                    $rootScope.toasterWarningInfo("请设置指派对象！");
                    return false;
                }
            }
            var data = [];
            angular.forEach($scope.info.dataList, function (req) {
                // req.createTime = $scope.info.time;
                req.type = $scope.info.issueItem.key;
                req.brdId = $scope.info.brdItem.brdId;
                req.groupTag = $scope.info.groupTag;
                req.creatorAccount = $scope.info.proposeUser.account;
                if ($scope.info.assignUser.account != undefined) {
                    req.toAccount = $scope.info.assignUser.account;
                }
                // console.log(req)
                // return false;
                data.push(req);
                if (req.sltUser) {
                    delete req.sltUser;
                }
                if (req.sltToAccount) {
                    delete req.sltToAccount;
                }
            });
            $modalInstance.close(data);
        };

        $scope.cancelClick = function () {
            $modalInstance.dismiss("cancel");
        };
    }
]);

app.controller("updateCtrl", [
    "$scope",
    "$rootScope",
    "$modalInstance",
    "$http",
    "$filter",
    "lodash",
    "items",
    function ($scope, $rootScope, $modalInstance, $http, $filter, lodash, items) {
        $scope.creator = {};
        $scope.issueList = [];
        $scope.sltAccount = {};
        $scope.userList = [];
        $scope.brdList = [];

        initValue();
        getIssueList();
        getUserList();
        setIssueStatus();

        //初始化数据
        function initValue() {
            angular.forEach(issueUser, function (user) {
                user.crtname = user.name;
            });
            items.userItem = {name: items.user, crtname: items.user};
            $scope.info = items;
            $scope.issueUser = issueUser;
        }

        //获取业务线数据
        function getIssueList() {
            var url = sys_config_url + "/select/get?type=BRD_BUSINESS";
            $http.get(url).success(function (result) {
                if (result.code == "0") {
                    if (result.data) {
                        angular.forEach(result.data.BRD_BUSINESS, function (issue) {
                            issue.crtname = issue.value;
                        });
                        items.typeItem = lodash.findWhere(result.data.BRD_BUSINESS, {
                            key: items.type
                        });
                        $scope.issueList = result.data.BRD_BUSINESS;
                    } else {
                        $rootScope.toasterErrorInfo("获取业务线数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
            });
        }

        //获取用户集合（设置提出人和指派对象）
        function getUserList() {
            var url = beetle_auth_url + "/user/?token_id=" + getTokenId();
            $http.get(url).success(function (result) {
                if (result.success) {
                    if (result.data) {
                        $scope.userList = result.data;
                        $scope.sltAccount = lodash.findWhere(result.data, {
                            account: $scope.info.toAccount
                        });
                        $scope.creator = lodash.findWhere(result.data, {
                            account: $scope.info.creatorAccount
                        });
                    } else {
                        $rootScope.toasterErrorInfo("获取用户数据错误！");
                    }
                } else {
                    $rootScope.toasterErrorInfo(result.msg);
                }
            });
        }

        //设置状态列表
        function setIssueStatus() {
            angular.forEach(cStatus, function (status) {
                status.crtname = status.name;
            });
            items.issueStatusItem = lodash.findWhere(cStatus, {key: items.status});
            $scope.issueStatus = cStatus;
        }

        //选择用户
        $scope.selectUserItem = function (item) {
            $scope.info.user = item.name;
        };

        //选择指派（单人）
        $scope.selectToAccount = function (user) {
            $scope.sltAccount = user;
        };

        //选择业务线
        $scope.selectTypeItem = function (item) {
            $scope.info.type = item.key;
        };

        //选择BRD
        $scope.selectBrdItem = function (item) {
            $scope.info.brdId = item.brdId;
        };

        //选择状态
        $scope.selectIssueStatusItem = function (item) {
            $scope.info.status = item.key;
            $scope.info.issueStatus =
                item.key == "INHAND" ? "新建" : item.key == "COMPLETE" ? "已完成" : "";
        };

        $scope.createClick = function () {
            $scope.info.toAccount = $scope.sltAccount.account;
            $scope.info.toName = $scope.sltAccount.realname;
            $modalInstance.close($scope.info);
        };

        $scope.cancelClick = function () {
            $modalInstance.dismiss("cancel");
        };
    }
]);

app.controller("designCtrl", [
    "$scope",
    "$rootScope",
    "$modalInstance",
    "$http",
    "$filter",
    "lodash",
    "data",
    "reqList",
    function (
        $scope,
        $rootScope,
        $modalInstance,
        $http,
        $filter,
        lodash,
        data,
        reqList
    ) {
        $scope.type = "induction";
        $scope.reqList = reqList;
        $scope.designTitle = "归纳产品设计";
        var filteredArray = new Array();
        angular.forEach(data, function (value) {
            var dataItem = lodash.findIndex(filteredArray, function (d) {
                return value.number == d.number;
            });
            if (dataItem == -1) {
                filteredArray.push(value);
            }
        });
        $scope.selectedTrData = filteredArray;

        $scope.createClick = function (data) {
            if (!data.design.title) {
                $(".form-input-title").focus();
                $rootScope.toasterWarningInfo("请输入标题");
                return false;
            }
            if (!data.design.desc) {
                $(".form-input-desc").focus();
                $rootScope.toasterWarningInfo("请输入描述");
                return false;
            }
            $modalInstance.close(data);
        };

        $scope.cancelClick = function () {
            lodash.remove(data, {isSave: false});
            $scope.selectedTrData = data;
            $modalInstance.dismiss("cancel");
        };
    }
]);
